﻿using Coldairarrow.Entity.Base;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_PrinterBusiness : BaseBusiness<PB_Printer>, IPB_PrinterBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public PB_PrinterBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<PB_PrinterDTO>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
         
            var search = input.Search;
            Expression<Func<PB_Printer, Base_Department,PB_WorkStation, Base_Department, Base_EnumItem, PB_PrinterDTO>> select = (a, b, c,d,e) => new PB_PrinterDTO
            {
                DepartmentName = b.Name,
                WorkshopName = d.Name,
                WorkStationName = c.Name,
                Typename=e.Name,
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<Base_Department>() on a.DeptId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<PB_WorkStation>() on a.WorkStationId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    join d in Db.GetIQueryable<Base_Department>() on a.WorkshopId equals d.Id into ad
                    from d in ad.DefaultIfEmpty()
                    join e in Db.GetIQueryable<Base_EnumItem>() on a.Type equals e.Value into ae
                    from e in ae.DefaultIfEmpty()
                    select @select.Invoke(a,b,c,d,e);
            if (!search.Keyword.IsNullOrEmpty())
            {
                var keyword = $"%{search.Keyword}%";
                q = q.Where(x =>
                      EF.Functions.Like(x.Name, keyword)
                      || EF.Functions.Like(x.Code, keyword)
                      ||EF.Functions.Like(x.Typename,keyword)
                      );
            }
            ///打印机权限
           q= q.Where(m => _operator.Property.DeptIds.Contains(m.DeptId));    
            return await q.GetPageResultAsync(new PageInput());
        }

        public async Task<List<PB_PrinterSelectDTO>> GetDataListByPrintServiceAsync(string deptId)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("SELECT ");
            sql.Append("Id,");
            sql.Append("Name ");
            sql.Append("FROM PB_Printer ");
            sql.Append("WHERE ");
            sql.Append("DeptId = '"+deptId+"'");

            return await Db.GetListBySqlAsync<PB_PrinterSelectDTO>(sql.ToString());
        }

        public async Task<PB_Printer> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(PB_Printer data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(PB_Printer data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}